<?php
define('ACC', true);
require('./include/init.php');
session_start(); 
//print_r($_SESSION);
$userid = $_SESSION['user_id'];
//$userid = 26;

$company = new companymodel();
$companyinfo = $company->getcompanyinfo($userid);
$companyid = $companyinfo['company_id']; 
$company_y = $companyinfo['company_y'];
$company_q = $companyinfo['company_q'];


$buildinginfo = $company->getallelements($companyid, 'building');  
$buildinglist = $company->getelementlist('building');

$cashinfo = $company->getcash_equivalent($companyid);
$ttlcashequivalent = 0;
foreach($cashinfo as $k=>$v){ 
    if(($k == '库存现金') || ($k == '银行存款')){
        $ttlcashequivalent = $ttlcashequivalent + $v;   
    }
}

echo '<div id="buildingupdate"><form><table class="table table-bordered">
          <tr><td>厂房类别</td><td>厂房价格</td><td>目前已有数量</td><td>目前资金还可购入</td><td>采购数量输入</td> </tr>';
foreach($buildinglist  as $k=>$v){
    $buildingname = $v['element_name'];
    $buildingprice =  $v['building_price'];
    
    $numofbuilding = 0;
    foreach($buildinginfo as $key =>$value){
         if($value['element_id'] == $v['element_id']){
             $numofbuilding++;
         } 
    }                                                                                                                                                                           
    echo'<tr><td>' . $buildingname . '</td><td>' . $buildingprice . '</td><td><div id ="numof' . $v['element_id'] . '">' . $numofbuilding . '</div></td><td><div id ="limitof' . $v['element_id'] . '">' .  floor($ttlcashequivalent / $buildingprice) .  '</div></td><td><input type="text" name="' . $buildingname. '" id = "' . $v['element_id'] . '" /></td></tr>' ;
}
 echo '</table></form></div><button type="button" id="purchase-building" class="btn btn-primary">确定</button><br/>';

 echo '          
<script>
$("#purchase-building").click(function(){
               $.post("purchasebuilding1.php", 
                       {';
foreach($buildinglist as $k=>$v){
    echo $v['element_id'] . ':$("#' . $v['element_id'] . '").val(),';
}     
echo '                       
                       },                 
                       function (data, textStatus){
                            $.each(data,function(name,value){ 
                            var anchor = "#" + name;
                            $(anchor).html(value);
                            })
                       },
                       "json",
                       window.location.reload()
               );
          });
</script>
';

?>
